import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login'
import layout from '@/views/layout'
import company from '@/views/company'
import library from '@/views/library'
import discover from '@/views/discover'
import personal from '@/views/personal'
import message from '@/views/personal/message'
import info from '@/views/personal/info'
import technicList from '@/views/discover/technicList.vue'
import technicDetail from '@/views/discover/technicDetail.vue'
import editInfo from '@/views/personal/editInfo'
import { getToken } from '@/utils/storage'
import { Toast } from 'vant'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: 'personal' },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/question/:city/:type',
    name: 'question',
    component: () => import('@/views/question/index.vue')
  },
  {
    path: '/',
    name: 'layout',
    component: layout,
    children: [
      { name: 'company', path: 'company', component: company },
      { name: 'library', path: 'library', component: library },
      { name: 'discover', path: 'discover', component: discover },
      { name: 'personal', path: 'personal', component: personal }
    ]
  },
  { name: 'message', path: '/message', component: message },
  { name: 'info', path: '/info', component: info },
  { name: 'editInfo', path: '/editinfo', component: editInfo },
  {
    path: '/technicList',
    name: 'technicList',
    component: technicList
  },
  {
    name: 'technicDetail',
    path: '/technicDetail/:id',
    component: technicDetail
  }
]

const router = new VueRouter({
  routes
})
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  if (whiteList.includes(to.path)) {
    next()
  } else {
    if (getToken()) {
      next()
    } else {
      Toast('请先登录')
      next('/login')
    }
  }
})

export default router
